<?php
namespace app\farm\model;

use app\BaseModel;
use app\shop\model\IntegralLog;
use app\member\model\Level;
use app\member\model\Rights;
use app\shop\model\Member;
use longbingcore\wxcore\PospalApi;
use longbingcore\wxcore\PushMsgModel;
use think\facade\Db;

class BalanceOrder extends BaseModel
{
    //定义表名
    protected $name = 'lbfarm_balance_order_list';

//
    protected $append = [

        'nick_name',


    ];


    /**
     * @author chenniang
     * @DataTime: 2021-07-11 15:47
     * @功能说明:用户昵称
     */
    public function getNickNameAttr($value,$data){

        if(!empty($data['user_id'])){

            $user_model = new User();

            return $user_model->where(['id'=>$data['user_id']])->value('nickName');

        }

    }













    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['status'] = 1;

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-22 11:31
     * @功能说明:订单支付回调
     */
    public function orderResult($order_code,$transaction_id){

        $order = $this->dataInfo(['order_code'=>$order_code]);

        if(!empty($order)&&$order['status']==1){

            $user_model = new User();

            $water_model= new BalanceWater();

            $user = $user_model->dataInfo(['id'=>$order['user_id']]);

            Db::startTrans();

            $update = [

                'transaction_id' => $transaction_id,

                'status'         => 2,

                'pay_time'       => time(),

                'now_balance'    => $order['true_price']+$user['balance']

            ];
            //修改订单信息
            $res = $this->dataUpdate(['id'=>$order['id']],$update);

            if($res==0){

                Db::rollback();

            }

            $update = [

                'balance' => $order['true_price']+$user['balance'],

            ];

            if(!empty($order['member_level'])){

                $update['member_level'] = $order['member_level'];
            }
            //修改用户余额
            $res = $user_model->dataUpdate(['id'=>$order['user_id']],$update);

            if($res==0){

                Db::rollback();

            }
            //添加余额流水
            $insert = [

                'uniacid' => $order['uniacid'],

                'user_id' => $order['user_id'],

                'price'   => $order['true_price'],

                'order_id'=> $order['id'],

                'add'     => 1,

                'type'    => 1,

                'before_balance' => $user['balance'],

                'after_balance'  => $order['true_price']+$user['balance'],
            ];

            $res = $water_model->dataAdd($insert);

            if($res==0){

                Db::rollback();

            }
            //增加积分
            $ilog_model = new IntegralLog();

            $ilog_model->integralUserAdd($order['user_id'],$order['integral'],$order['uniacid'],2,2,$order['id']);

            if(!empty($order['member_level'])){

                $member_model = new Member();

                $order['member_title'] = $member_model->where(['id'=>$order['member_level']])->value('title');

                $push_model = new PushMsgModel($order['uniacid']);

                $push_model->sendMsg($order,16);
            }

            Db::commit();

        }

        return true;

    }





}